package ut.servlet.member;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class CreateProfile extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		// 取得並驗證使用者輸入資訊
		String description, education, certification, experience;
		String[] from, to;
		if(request.getParameter("description") == null || request.getParameter("education") == null || request.getParameter("certification") == null || request.getParameter("experience") == null || request.getParameter("from_lan") == null || request.getParameter("to_lan") == null){
			return;
		}else{
			description = request.getParameter("description");
			education = request.getParameter("education");
			certification = request.getParameter("certification");
			experience = request.getParameter("experience");
			if(description.length() > 255 || education.length() > 255 || certification.length() > 255 || experience.length() > 255){
				return;
			}
			// 處理跳脫字元
			description = description.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			education = education.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			certification = certification.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			experience = experience.replace("&", "&amp;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
			// 處理換行字元
			description = description.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			education = education.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			certification = certification.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			experience = experience.replace("\r\n", "<br/>").replace("\n", "<br/>").replace("\r", "<br/>");
			// 處理專長
			if(!request.getParameter("from_lan").matches("[0-9]+(,[0-9]{1,})*") || !request.getParameter("to_lan").matches("[0-9]+(,[0-9]{1,})*")){
				return;
			}
			from = request.getParameter("from_lan").split(",");
			to = request.getParameter("to_lan").split(",");
			if(from.length != to.length){
				return;
			}
			for(int i = 0; i < from.length; i++){
				if(ut.res.Language.getLanguage(Integer.parseInt(from[i])) == null || ut.res.Language.getLanguage(Integer.parseInt(to[i])) == null || from[i].equals(to[i])){
					return;
				}
			}
		}
		// 確認使用者已經完成信箱驗證
		ut.data.User user = (ut.data.User)request.getSession().getAttribute("user");
		if(user.verify == 0){
			return;
		}
		// 將新的案主資料加入資料庫
		response.setHeader("result", ut.data.Profile.createProfile(user.id, description, education, certification, experience, from, to) + ""); // true: 成功、false: 失敗
	}
}